package com.example.medicalzb.service;

import com.example.medicalzb.entity.MedicalPolicyEntity;
import com.example.medicalzb.mapper.MedicalPolicyMapper;
import com.example.medicalzb.model.MedicalPolicyModel;
import com.example.medicalzb.param.MedicalPolicyParam;
import com.example.medicalzb.util.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class MedicalPolicyService {
    @Autowired
    private MedicalPolicyMapper medicalPolicyMapper;
    public Msg getMedicalPolicyWithPage(MedicalPolicyParam medicalPolicyParam) {
        PageHelper.startPage(medicalPolicyParam.getPn(),medicalPolicyParam.getSize());
        List<MedicalPolicyModel> list = medicalPolicyMapper.getAllMedicalPolicy(medicalPolicyParam);
        PageInfo<MedicalPolicyModel> pageInfo = new PageInfo<>(list,5);
        return Msg.success().data("policyInfo",pageInfo);
    }

    public Msg saveMedicalPolicy(MedicalPolicyParam medicalPolicyParam) {
        MedicalPolicyEntity medicalPolicyEntity = new MedicalPolicyEntity();
        BeanUtils.copyProperties(medicalPolicyParam,medicalPolicyEntity);
//        System.out.println(medicalPolicyParam.getTotal() + medicalPolicyParam.getMessage());
        medicalPolicyEntity.setCreateTime(new DateTime().toString("yyyy-MM-dd"));
        medicalPolicyEntity.setUpdateTime(new DateTime().toString("yyyy-MM-dd"));
        //System.out.println(medicalPolicyEntity.getTitle() + medicalPolicyEntity.getMessage());
        int i = medicalPolicyMapper.saveMedicalPolicy(medicalPolicyEntity);
        List<MedicalPolicyModel> list = medicalPolicyMapper.getAllMedicalPolicy(null);
        medicalPolicyEntity.setTotal((long) list.size());
        if (i > 0){
            Long num = medicalPolicyEntity.getTotal() % 5 == 0 ? (medicalPolicyEntity.getTotal() / 5) : ((medicalPolicyEntity.getTotal() / 5) + 1);
            return Msg.success().mess("添加医保政策成功").data("numberOfAdd",i).data("pages",num);
        }
        return Msg.fail().mess("添加医保政策失败");
    }

    public Msg updateMedicalPolicy(Long id, MedicalPolicyParam policyParam) {
        policyParam.setUpdateTime(new DateTime().toString("yyyy-MM-dd"));
        policyParam.setId(id);
        MedicalPolicyEntity medicalPolicyEntity = new MedicalPolicyEntity();
        BeanUtils.copyProperties(policyParam,medicalPolicyEntity);
        int i = medicalPolicyMapper.updateMedicalPolicy(medicalPolicyEntity);
        if (i > 0){
            return Msg.success().mess("修改医保政策成功").data("updateData",medicalPolicyEntity);
        }
        return Msg.fail().mess("修改医保政策失败");
    }

    public Msg deleteMedicalPolicy(Long id) {
        int i = medicalPolicyMapper.deleteMedicalPolicy(id);
        if (i > 0){
            return Msg.success().mess("删除成功").data("numberOfMPolicyDelete",i);
        }
        return Msg.fail().mess("删除失败");
    }
}
